package request;

import core.*;
import response.ResponseLogin;
import java.io.OutputStream;

import java.util.List;
/*man-
import response.ResponseBuddyOnline;
import response.ResponseCreate;
import response.ResponseCreateMap;
import response.ResponseLogin;
import response.ResponsePartyOnline;
import response.ResponsePartyUpdate;
import response.ResponseQuestionLog;
import response.ResponseQuestionRatio;
*/

/**
 * Logs in the client character.
 *
 * @author eric, yiyi, Zoran, Jason, Gary
 */
public class RequestLogin extends GameRequest {

    // User Information
    private GameUser user;
    private GameCharacter character;
    // Request Information
    private GameMap map;
    private String version=null;
    private String username=null;
    private String password=null;
    private int party_id;
    private short status;
    private boolean isValid;

    public RequestLogin() {
        code = Constants.CMSG_AUTH;
    }

    public void parse() {
        user = client.getUser();

        if (user == null) {
            version = client.readString(dataIn);
            gameServer.log("version is :"+version);
            if (version.trim().compareTo(Constants.CLIENT_VERSION) >= 0) {
                username = client.readString(dataIn);
                gameServer.log("user is :"+username);
                password = client.readString(dataIn);
                gameServer.log("password is :"+password);
                
                user = new GameUser(-1);
                status = gameServer.login(username, password, user);

                if (status == 0) {
                    
                    gameServer.getGameDB().setLastLoginIP(client.getClientIP(), user.getID());
                    user.setLastSaveTime(System.currentTimeMillis());

                    client.setUser(user);
                    gameServer.log("Connection Successful!");

                    if (user.getID() != Constants.BUG_USER_ID) {
                        character = gameServer.selectCharacter(user.getID(), 0);

                        if (character != null) {
                           /**
                            map = gameServer.getMap(character.getMapID());

                            if (map == null) {
                                character.setMapID(Constants.DEFAULT_MAP);
                                character.setPos(Constants.DEFAULT_POSITION);

                                map = gameServer.getMap(character.getMapID());
                            }*/

                            user.setCharacter(character);

                            party_id = character.getPartyID();
                             /*
                            if (party_id != 0) {
                                gameServer.setParty(party_id, character);
                            }

                            character.startHealthRegenTimer(5000);*/

                            gameServer.log(character.getName() + " has entered the game.");
                        }
                    }
                } else {
                    user = null;
                }
           } else {
                status = 3;
            }

            isValid = true;
        }
    }

    public void respond(OutputStream output) {
        if (isValid) {
            ResponseLogin response = new ResponseLogin();
            response.set(gameServer);
            response.setOut(output);
            response.setCharacter(character);
            //man- response.setMap(map);
            response.setStatus(status);
            response.setUser(user);
            response.run();
        }
            
            if (status == 0) {
                /*man-
                if (user.getID() != Constants.BUG_USER_ID) {
                    ResponseCreate createResponse = new ResponseCreate();
                    createResponse.set(gameServer);
                    createResponse.setCharacter(character);
                    createResponse.setUser(user);

                    gameServer.updateOthersQueue(createResponse, gameServer.getActiveUserList(character.getMapID()), user.getID());

                    for (GameCharacter buddy : gameServer.getGameDB().getBuddyOnlineList(character.getID(), true)) {
                        ResponseBuddyOnline buddyResponse = new ResponseBuddyOnline();
                        buddyResponse.set(gameServer);
                        buddyResponse.setCharacter(character);

                        gameServer.updateUserQueue(buddyResponse, gameServer.getActiveUser(buddy.getUserID()));
                    }

                    if (party_id != 0) {
                        if (gameServer.getParty(party_id).getPartyList().size() > 1) {
                            ResponsePartyOnline partyResponse = new ResponsePartyOnline();
                            partyResponse.set(gameServer);
                            partyResponse.setCharacter(character);

                            gameServer.updateOthersQueue(partyResponse, gameServer.getActivePartyUserList(party_id), user.getID());

                            ResponsePartyUpdate updateResponse = new ResponsePartyUpdate();
                            updateResponse.set(gameServer);
                            updateResponse.setCharacter(character);

                            gameServer.updateOthersQueue(updateResponse, gameServer.getActivePartyUserList(party_id), user.getID());
                        }
                    }

                    ResponseQuestionRatio ratioResponse = new ResponseQuestionRatio();
                    ratioResponse.setOut(output);
                    ratioResponse.setRatio(character.getQuestionRatio());
                    ratioResponse.run();

                    List<GameQuestion> questionList = gameServer.getGameDB().getQuestionLog(character.getID(), 30);

                    if (!questionList.isEmpty()) {
                        ResponseQuestionLog questionResponse = new ResponseQuestionLog();
                        questionResponse.set(gameServer);
                        questionResponse.setOut(output);
                        questionResponse.setQuestionList(questionList);
                        questionResponse.run();
                    }
                } else {
                    for (GameMap mapInstance : gameServer.getMapList()) {
                        ResponseCreateMap mapResponse = new ResponseCreateMap();
                        mapResponse.set(gameServer);
                        mapResponse.setOut(output);
                        mapResponse.setMap(mapInstance);
                        mapResponse.run();
                    }
                }*/
            }
    }
}
